<template>
  <div class="dashboard">
    <!-- 统计概览 -->
    <div class="stats-grid">
      <el-card
        v-for="stat in overviewStats"
        :key="stat.title"
        class="stat-card"
        shadow="hover"
      >
        <div class="stat-content">
          <div class="stat-icon" :style="{ backgroundColor: stat.color + '20' }">
            <el-icon :style="{ color: stat.color }" :size="32">
              <component :is="stat.icon" />
            </el-icon>
          </div>
          <div class="stat-info">
            <div class="stat-value">{{ stat.value }}</div>
            <div class="stat-title">{{ stat.title }}</div>
            <div class="stat-trend" :class="stat.trend > 0 ? 'up' : 'down'">
              <el-icon><ArrowUp v-if="stat.trend > 0" /><ArrowDown v-else /></el-icon>
              {{ Math.abs(stat.trend) }}%
            </div>
          </div>
        </div>
      </el-card>
    </div>

    <!-- 图表区域 -->
    <el-row :gutter="20" class="charts-row">
      <el-col :span="12">
        <el-card title="模块类型分布" shadow="never">
          <template #header>
            <div class="card-header">
              <span>模块类型分布</span>
            </div>
          </template>
          <ModuleTypeChart />
        </el-card>
      </el-col>
      <el-col :span="12">
        <el-card title="依赖关系图" shadow="never">
          <template #header>
            <div class="card-header">
              <span>依赖关系图</span>
            </div>
          </template>
          <DependencyChart />
        </el-card>
      </el-col>
    </el-row>

    <el-row :gutter="20" class="charts-row">
      <el-col :span="16">
        <el-card title="模块大小分布" shadow="never">
          <ModuleSizeChart />
        </el-card>
      </el-col>
      <el-col :span="8">
        <el-card title="TOP模块" shadow="never">
          <TopModulesList />
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { ArrowUp, ArrowDown, Grid, DataBoard, Link, Monitor } from '@element-plus/icons-vue'
import { useModulesStore } from '@/stores/modules'
import ModuleTypeChart from '@/components/charts/ModuleTypeChart.vue'
import DependencyChart from '@/components/charts/DependencyChart.vue'
import ModuleSizeChart from '@/components/charts/ModuleSizeChart.vue'
import TopModulesList from '@/components/TopModulesList.vue'

const modulesStore = useModulesStore()

// 计算属性
const overviewStats = computed(() => {
  const dashboard = modulesStore.dashboardData

  // 如果有真实的dashboard数据，使用它
  if (dashboard && dashboard.overviewStats) {
    const stats = dashboard.overviewStats
    return [
      {
        title: '总模块数',
        value: stats.totalModules,
        icon: 'Grid',
        color: '#409EFF',
        trend: stats.trends.totalModules_trend
      },
      {
        title: '依赖总数',
        value: stats.totalDeps,
        icon: 'Link',
        color: '#67C23A',
        trend: stats.trends.totalDeps_trend
      },
      {
        title: '总大小',
        value: formatBytes(stats.totalSize),
        icon: 'DataBoard',
        color: '#E6A23C',
        trend: stats.trends.totalSize_trend
      },
      {
        title: '动态库',
        value: stats.dylibCount,
        icon: 'Monitor',
        color: '#F56C6C',
        trend: stats.trends.dylibCount_trend
      }
    ]
  }

  // 如果没有真实数据，使用模拟数据
  const modules = modulesStore.modules
  if (modules.length === 0) {
    return [
      {
        title: '总模块数',
        value: 0,
        icon: 'Grid',
        color: '#409EFF',
        trend: 0
      },
      {
        title: '依赖总数',
        value: 0,
        icon: 'Link',
        color: '#67C23A',
        trend: 0
      },
      {
        title: '总大小',
        value: '0 B',
        icon: 'DataBoard',
        color: '#E6A23C',
        trend: 0
      },
      {
        title: '动态库',
        value: 0,
        icon: 'Monitor',
        color: '#F56C6C',
        trend: 0
      }
    ]
  }

  // 使用模块数据计算统计
  const totalModules = modules.length
  const totalDeps = modules.reduce((sum, m) => sum + (m.deps || 0), 0)
  const totalSize = modules.reduce((sum, m) => sum + (m.size || 0), 0)
  const dylibs = modules.filter(m => m.name.includes('.dylib')).length

  return [
    {
      title: '总模块数',
      value: totalModules,
      icon: 'Grid',
      color: '#409EFF',
      trend: 0
    },
    {
      title: '依赖总数',
      value: totalDeps,
      icon: 'Link',
      color: '#67C23A',
      trend: 0
    },
    {
      title: '总大小',
      value: formatBytes(totalSize),
      icon: 'DataBoard',
      color: '#E6A23C',
      trend: 0
    },
    {
      title: '动态库',
      value: dylibs,
      icon: 'Monitor',
      color: '#F56C6C',
      trend: 0
    }
  ]
})

const formatBytes = (bytes) => {
  if (!bytes) return '0 B'
  const k = 1024
  const sizes = ['B', 'KB', 'MB', 'GB']
  const i = Math.floor(Math.log(bytes) / Math.log(k))
  return parseFloat(((bytes / Math.pow(k, i)) || 0).toFixed(2)) + ' ' + sizes[i]
}

onMounted(() => {
  modulesStore.initialize()
})
</script>

<style scoped lang="scss">
.dashboard {
  .stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    gap: 20px;
    margin-bottom: 20px;

    .stat-card {
      .stat-content {
        display: flex;
        align-items: center;
        gap: 20px;

        .stat-icon {
          width: 64px;
          height: 64px;
          border-radius: 16px;
          display: flex;
          align-items: center;
          justify-content: center;
        }

        .stat-info {
          flex: 1;

          .stat-value {
            font-size: 32px;
            font-weight: 700;
            color: var(--el-text-color-primary);
            line-height: 1;
          }

          .stat-title {
            font-size: 16px;
            color: var(--el-text-color-secondary);
            margin: 4px 0 2px 0;
          }

          .stat-trend {
            display: flex;
            align-items: center;
            gap: 4px;
            font-size: 14px;
            font-weight: 500;

            &.up {
              color: #67C23A;
            }

            &.down {
              color: #F56C6C;
            }
          }
        }
      }
    }
  }

  .charts-row {
    margin-bottom: 20px;

    .el-card {
      height: 400px;
    }
  }

  .card-header {
    font-weight: 600;
    color: var(--el-text-color-primary);
  }
}

// 响应式设计
@media (max-width: 768px) {
  .dashboard {
    .stats-grid {
      grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
      gap: 12px;

      .stat-card .stat-content {
        flex-direction: column;
        text-align: center;
        gap: 12px;
      }
    }

    .charts-row .el-card {
      height: 300px;
      margin-bottom: 12px;
    }
  }
}
</style>